﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;

namespace BackEnd.Controllers
{
    public class MenuController : Controller
    {
        [CustomAuthorize(Action = Constants.AdminActions.ListMenu)]
        public ActionResult Index()
        {
            MenuModel menuModel = new MenuModel();
            menuModel.ListMenu = new MenuBL().GetAll();
            return View(menuModel);
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateMenu)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateMenu)]
        public ActionResult Create(MenuModel menuModel)
        {
            if (string.IsNullOrWhiteSpace(menuModel.MenuName))
            {
                ModelState.AddModelError("MenuName", "MenuName is not null");
            }
            if (string.IsNullOrWhiteSpace(menuModel.MenuName))
            {
                ModelState.AddModelError("RedirectUrl", "RedirectUrl is not null");
            }
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new MenuBL().Insert(new Menu { MenuName = menuModel.MenuName, RedirectUrl=menuModel.RedirectUrl, Order=menuModel.Order, Status=menuModel.Status });
                return RedirectToAction("Index", "Menu");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditMenu)]
        public ActionResult Edit(int id)
        {
            MenuModel menuModel = new MenuModel();
            Menu menu = new MenuBL().GetById(id);
            if (menu != null)
            {
                menuModel.Id = menu.Id;
                menuModel.MenuName = menu.MenuName;
                menuModel.RedirectUrl = menu.RedirectUrl;
                menuModel.Order = menu.Order;
                menuModel.Status = menu.Status;
            }
            return View(menuModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditMenu)]
        public ActionResult Edit(MenuModel menuModel)
        {
            if (string.IsNullOrWhiteSpace(menuModel.MenuName))
            {
                ModelState.AddModelError("MenuName", "MenuName is not null");
            }
            if (string.IsNullOrWhiteSpace(menuModel.MenuName))
            {
                ModelState.AddModelError("RedirectUrl", "RedirectUrl is not null");
            }
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new MenuBL().Update(new Menu { Id = menuModel.Id, MenuName = menuModel.MenuName, RedirectUrl = menuModel.RedirectUrl, Order = menuModel.Order, Status = menuModel.Status });
                return RedirectToAction("Index", "Menu");
            }
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteMenu)]
        public JsonResult Delete(string listId)
        {            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new MenuBL().Delete(Convert.ToInt32(arrayId[i]));
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
